//
//  HMComposeBottomVIew.swift
//  HMSlender
//
//  Created by 赵少朋赵少朋 on 2017/2/10.
//  Copyright © 2017年 ITheima. All rights reserved.
//

import UIKit


enum HMComposeTextViewBtnType: Int {

    case picture = 101
    case mention = 102
    case trend = 103
    case emoticon = 104
    case add = 105
    
}

class HMComposeBottomVIew: UIView {

    var emoticonBtn:UIButton?
    
    var isEmoticon:Bool? {
    
        didSet {
        
            if isEmoticon! {
            
                self.emoticonBtn?.setImage(UIImage(named:"compose_keyboardbutton_background"), for: UIControlState.normal)
            self.emoticonBtn?.setImage(UIImage(named:"compose_keyboardbutton_background_highlighted"), for: UIControlState.highlighted)
            } else {
            
                self.emoticonBtn?.setImage(UIImage(named:"compose_emoticonbutton_background"), for: UIControlState.normal)
                self.emoticonBtn?.setImage(UIImage(named:"compose_emoticonbutton_background_highlighted"), for: UIControlState.highlighted)
            }
        }
    }
    
    // 声明一个闭包
    var closure:((HMComposeTextViewBtnType)->())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func  setupUI() -> Void {
        
        let pictureBtn = addChildButtons(imgName: "compose_toolbar_picture" , btnType:.picture)
        let mentionBtn = addChildButtons(imgName: "compose_mentionbutton_background", btnType:.mention)
        let trendBtn = addChildButtons(imgName: "compose_trendbutton_background", btnType:.trend)
        let emoticonBtn = addChildButtons(imgName: "compose_emoticonbutton_background", btnType:.emoticon)
        let addBtn = addChildButtons(imgName: "compose_add_background", btnType:.add)
        
        self.emoticonBtn = emoticonBtn
        // 添加约束
        pictureBtn.snp_makeConstraints { (make) in
            make.left.top.bottom.equalTo(self)
            make.width.equalTo(mentionBtn)
        }
        
        mentionBtn.snp_makeConstraints { (make) in
            make.top.bottom.equalTo(self)
            make.left.equalTo(pictureBtn.snp_right)
            make.width.equalTo(trendBtn)
        }
        
        trendBtn.snp_makeConstraints { (make) in
            make.top.bottom.equalTo(self)
            make.left.equalTo(mentionBtn.snp_right)
            make.width.equalTo(emoticonBtn)
        }
        
        emoticonBtn.snp_makeConstraints { (make) in
            make.top.bottom.equalTo(self)
            make.left.equalTo(trendBtn.snp_right)
            make.width.equalTo(addBtn)
        }
        
        addBtn.snp_makeConstraints { (make) in
            make.left.equalTo(emoticonBtn.snp_right)
            make.top.right.bottom.equalTo(self)
        }

    }
    
    // MARK: 按钮点击事件
    func bottomViewBtnClick(button:UIButton) -> Void {
        
        //点击按钮后执行闭包 让控制器执行对应事件
        closure?(HMComposeTextViewBtnType(rawValue: button.tag)!)
    }
    
    // 抽取创建按钮方法
    func addChildButtons(imgName:String, btnType:HMComposeTextViewBtnType) -> UIButton {
        
        let btn = UIButton()
        
        btn.addTarget(self, action: #selector(bottomViewBtnClick), for: UIControlEvents.touchUpInside)
        //设置tag值
        btn.tag = btnType.rawValue
        
        btn.setImage(UIImage(named:imgName), for: UIControlState.normal)
        btn.setImage(UIImage(named:"\(imgName)_highlighted"), for: UIControlState.highlighted)
        btn.setBackgroundImage(UIImage(named:"compose_toolbar_background"), for: UIControlState.normal)
        btn.setBackgroundImage(UIImage(named:"compose_toolbar_background"), for: UIControlState.highlighted)
        
        addSubview(btn)
        return btn
    }
    
    
    
}
